<template>
  <div class="app-container">
    <el-card>
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="openDialog(-1)"
      >添加团队
      </el-button>
      <el-dialog
        title="团队信息"
        :destroy-on-close="true"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="dialogVisible"
        top="3vh"
        width="60%"
        @close="refresh"
      >
        <add-or-update-team
          :key="dialogVisible"
          :team-id="teamId"
          @updateDialog="updateDialog"
        />
      </el-dialog>
      <el-input
        v-model="searchName"
        placeholder="团队标题 | 描述"
        style="width: 10rem;margin-left: 3rem"
        @keyup.enter.native="teamListAdmin"
        @input="teamListAdmin"
      />
    </el-card>
    <el-card style="margin-top: 1rem;">
      <el-table
        :data="tableData"
        stripe
      >
        <el-table-column prop="nick" label="创建者" />
        <el-table-column prop="title" label="标题" />
        <el-table-column prop="privilege" label="权限">
          <template slot-scope="scope">
            <span :style="{color:scope.row.privilege===0?'#1abc9c':'#3742fa'}">
              {{ scope.row.privilege === 0 ? 'Public' : 'Private' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-link :type="scope.row.status===0?'success':'danger'" @click="updateTeamStatus(scope.row)">
              {{ scope.row.status === 0 ? 'Available' : 'Reserved' }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-link icon="el-icon-edit" type="primary" @click="openDialog(scope.row.teamId)">修改</el-link>
            <el-link
              icon="el-icon-delete"
              style="margin-left: 0.5rem"
              type="danger"
              @click="deleteTeam(scope.row.teamId)"
            >删除
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="消息管理">
          <template slot-scope="scope">
            <el-link icon="el-icon-setting" type="primary" @click="editTeam(scope.row)">团队数据</el-link>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 20px;"
        background
        :current-page="currentPage"
        :page-sizes="[10, 25, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
import { deleteTeam, teamListAdmin, updateTeamStatus } from '@/api/team'
import AddOrUpdateTeam from '@/views/admin/record/list/components/AddOrUpdateTeam'

export default {
  components: {
    AddOrUpdateTeam
  },
  data() {
    return {
      dialogVisible: false,
      teamId: -1,
      currentPage: 1,
      pageSize: 10,
      totalNum: 0,
      searchName: '',
      tableData: []
    }
  },
  created() {
    this.teamListAdmin()
  },
  methods: {
    teamListAdmin() {
      teamListAdmin({
        current: this.currentPage,
        size: this.pageSize,
        name: this.searchName
      }).then(res => {
        this.totalNum = parseInt(res.data.total)
        this.tableData = res.data.records
      })
    },
    updateTeamStatus(row) {
      const formData = new FormData()
      formData.set('teamId', row.teamId)
      formData.set('status', row.status)
      updateTeamStatus(formData).then(res => {
        if (res.data) {
          row.status = row.status ^ 1
          this.$message({ message: res.message, type: 'success' })
        }
      })
    },
    editTeam(row) {
      this.$router.push({
        name: 'AdminRecordData',
        params: { teamId: row.teamId }
      })
    },
    deleteTeam(teamId) {
      this.$confirm('此操作将永久删除团队及其数据，是否继续删除？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const form = new FormData()
        form.set('teamId', teamId)
        deleteTeam(form).then(res => {
          this.$message.success(res.message)
          this.teamListAdmin()
        })
      })
    },
    updateDialog(flag) {
      this.dialogVisible = flag
    },
    openDialog(teamId) {
      this.teamId = teamId
      this.dialogVisible = true
    },
    handleSizeChange(size) {
      this.pageSize = size
      this.teamListAdmin()
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
      this.teamListAdmin()
    },
    refresh() {
      this.teamListAdmin()
    }
  }
}
</script>

<style scoped>

</style>
